<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: ConfigMan.php 323 2008-12-01 18:32:53Z steen.rabol@gmail.com $
 *
 */

class ConfigMan extends PortalAdminPage
{
	const CONFIG_FILE = 'Application.Data.Settings';

	public function onLoad($param)
	{
		parent::onLoad($param);

		$this->setAdminPageTitle('Site Configuration');

		if($this->IsPostBack)
		{
			return;
		}

		$parameters = $this->Application->Parameters;

		// General Settings
		$this->SiteTitle->Text		= $parameters['SiteTitle'];
		$this->SiteSubTitle->Text	= $parameters['SiteSubTitle'];
		$this->SiteOwner->Text		= $parameters['SiteOwner'];
		$this->AdminEmail->Text		= $parameters['AdminEmail'];

		$themes = PortalUtil::getAvailablePortalThemes();
		$this->ThemeName->DataSource = $themes;
		$this->ThemeName->dataBind();
		$this->ThemeName->SelectedValue = (array_search($parameters['ThemeName'], $themes) ? array_search($parameters['ThemeName'], $themes) : 0);

		$adminthemes = PortalUtil::getAvailableAdminThemes();
		$this->AdminThemeName->DataSource = $adminthemes;
		$this->AdminThemeName->dataBind();
		$this->AdminThemeName->SelectedValue = (array_search($parameters['AdminThemeName'], $adminthemes) ? array_search($parameters['AdminThemeName'], $adminthemes) : 0);

		$roles = $this->DataAccess->createCommand("select id,name from tblroles")->query();

		$this->AdminRole->DataSource = $roles;
		$this->AdminRole->dataBind();
		$this->AdminRole->SelectedValue = TPropertyValue::ensureInteger($parameters['AdminRole']);

		$this->MinRole->DataSource = $this->DataAccess->createCommand("select id,name from tblroles")->query();
		$this->MinRole->dataBind();
		$this->MinRole->SelectedValue = TPropertyValue::ensureString($parameters['MinRole']);

		$this->NewUserRole->DataSource = $this->DataAccess->createCommand("select id,name from tblroles")->query();
		$this->NewUserRole->dataBind();
		$this->NewUserRole->SelectedValue = ($parameters['NewUserRole'] ? TPropertyValue::ensureInteger($parameters['NewUserRole']) : 0);

		$this->DefaultLanguage->DataSource = $this->DataAccess->createCommand("select * from tbllanguages where active=1 order by name")->query();
		$this->DefaultLanguage->dataBind();

		$sellang = $this->DataAccess->createCommand("select id from tbllanguages where culture='" . $parameters['DefaultLanguage'] . "'")->queryScalar();

		if($sellang)
		{
			$this->DefaultLanguage->SelectedValue = $sellang;
		}

		$this->AuthMode->SelectedValue	= TPropertyValue::ensureInteger($parameters['AuthMode']);
		$this->AppMode->SelectedValue	= TPropertyValue::ensureInteger($parameters['AppMode']);
		$this->ValidationKey->Text		= $parameters['ValidationKey'];
	}

	public function saveButtonClicked($sender, $param)
	{
		$dom = new TXmlDocument;

		if($dom->loadFromFile(Prado::getPathOfNamespace(self::CONFIG_FILE, '.xml')))
		{
			if(!$this->UpdateParameter($dom,'SiteTitle', $this->SiteTitle->Text))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('SiteTitle', $this->SiteTitle->Text);
			}

			if(!$this->UpdateParameter($dom,'SiteSubTitle', $this->SiteSubTitle->Text))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('SiteSubTitle', $this->SiteSubTitle->Text);
			}

			if(!$this->UpdateParameter($dom,'SiteOwner', $this->SiteOwner->Text))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('SiteOwner', $this->SiteOwner->Text);
			}

			if(!$this->UpdateParameter($dom,'AdminEmail', $this->AdminEmail->Text))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('AdminEmail', $this->AdminEmail->Text);
			}

			if(!$this->UpdateParameter($dom,'ThemeName', $this->ThemeName->SelectedItem->Text))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('ThemeName', $this->ThemeName->SelectedItem->Text);
			}

			if(!$this->UpdateParameter($dom,'AdminThemeName', $this->AdminThemeName->SelectedItem->Text))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('AdminThemeName', $this->AdminThemeName->SelectedItem->Text);
			}

			if(!$this->UpdateParameter($dom,'AdminRole', $this->AdminRole->getSelectedValue()))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('AdminRole', $this->AdminRole->getSelectedValue());
			}

			if(!$this->UpdateParameter($dom,'MinRole', $this->MinRole->getSelectedValue()))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('MinRole', $this->MinRole->getSelectedValue());
			}

			if(!$this->UpdateParameter($dom,'NewUserRole', $this->NewUserRole->getSelectedValue()))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('NewUserRole', $this->NewUserRole->getSelectedValue());
			}

			if(!$this->UpdateParameter($dom,'PortalLayout', 'PortalLayout'))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('PortalLayout', 'PortalLayout');
			}

			if(!$this->UpdateParameter($dom,'PortalAdminLayout', 'PortalAdminLayout'))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('PortalAdminLayout', 'PortalAdminLayout');
			}

			$deflangid	= $this->DefaultLanguage->SelectedValue;
			$defculture = $this->DataAccess->createCommand("select culture from tbllanguages where id=$deflangid")->queryScalar();

			if(!$this->UpdateParameter($dom,'DefaultLanguage', $defculture))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('DefaultLanguage', $defculture);
			}

			if(!$this->UpdateParameter($dom,'AuthMode', $this->AuthMode->getSelectedValue()))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('AuthMode', $this->AuthMode->getSelectedValue());
			}

			if(!$this->UpdateParameter($dom,'ValidationKey', $this->ValidationKey->Text))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('ValidationKey', $this->AuthMode->Text);
			}

			if(!$this->UpdateParameter($dom,'AppMode', $this->AppMode->getSelectedValue()))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('AppMode', $this->AppMode->getSelectedValue());
			}

			if(!$this->UpdateParameter($dom,'IgnoreInstallDir', '1'))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('IgnoreInstallDir', '1');
			}

			if(!$this->UpdateParameter($dom,'RunMode',"Normal"))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('RunMode', "Normal");
			}

			if(!$this->UpdateParameter($dom,'PortalVersion',$this->Application->PortalVersion()))
			{
				// Create paramtere
				$elements = $dom->Elements;
				$elements[]	= $this->createParameter('PortalVersion', $this->Application->PortalVersion());
			}

			$dom->saveToFile(Prado::getPathOfNamespace(self::CONFIG_FILE, '.xml'));
		}
		else
		{
			$this->reportError("failed to load :" . Prado::getPathOfNamespace(self::CONFIG_FILE, '.xml'));
		}

		$this->Application->DeleteConfigCache();
		$this->gotoDefaultPage();
	}

	public function UpdateParameter(&$dom,$id,$value)
	{
		$parameters = &$dom->getElementsByTagName('parameter');
		foreach ($parameters as $param)
		{
			if($id == $param->getAttribute("id"))
			{
				$param->setAttribute('value',$value);
				return true;
			}
		}

		return false;
	}

	private function createParameter($id, $value)
	{
		$element = new TXmlElement('parameter');
		$element->Attributes['id'] = $id;
		$element->Attributes['value'] = TPropertyValue::ensureString($value);
		return $element;
	}
}

?>